package com.shadow.demo.decompilation;

import java.io.*;
import java.util.Arrays;
import java.util.Objects;

public class Step1GetJar {

    // 层深
    public static int INDEX = 0;
    public static int ITEM_INDEX = 0;
    public static int JAR_INDEX = 0;

    // 依赖仓库到jar依赖
    public static void main(String[] args) {
        File rootDir = new File(Config.RESOURCE_DIRECTORY);
        scanDirectory(rootDir);
    }

    public static void scanDirectory(final File dir) {
        if (!dir.exists() || !dir.isDirectory()) {
            System.out.println("指定的路径不是一个有效的目录");
            return;
        }
        String type = checkDirectory(dir);
        System.out.println("开始扫描: " + dir.getName() +" 检查结果: " + type);
        handlerSwitch(type, dir);
        System.out.println("---------- ITEM_INDEX " + ++ITEM_INDEX);
    }

    public static void handlerVersion(File dir) {
        File targetDir = null;
        for (File file : Objects.requireNonNull(dir.listFiles())) {
            if (isVersionDir(file)) {
                targetDir = file;
            }
        }
        if (targetDir != null) {
            System.out.println("---开始处理第" + ++JAR_INDEX + "个jar" + targetDir.getPath());
            File jarFile = getJarFile(targetDir);
            if (jarFile == null) {
                System.out.println("---没找到对应jar");
                File pomFile = getPomFile(targetDir);
                if (pomFile == null) {
                    System.out.println("---没找到对应pom");
                } else {
                    System.out.println("---找到对应pom " + pomFile.getPath());
                    FileUtils.copyFile(pomFile, new File(Config.TARGET1_DIRECTORY, pomFile.getName()));
                }
            } else {
                System.out.println("---找到对应jar " + jarFile.getPath());
                FileUtils.copyFile(jarFile, new File(Config.TARGET1_DIRECTORY, jarFile.getName()));
            }
            System.out.println("--------------------------------");
        }
    }

    public static void handlerNormal(File dir) {
        scanDirectory(dir);
    }

    public static void handlerSwitch(final String type, File dir) {
        if (Objects.isNull(type)) {
            return;
        }
        if (type.equals("一般目录")) {
            ++INDEX;
            System.out.println("INDEX" + INDEX);
            Arrays.asList(Objects.requireNonNull(dir.listFiles())).forEach(Step1GetJar::handlerNormal);
            INDEX--;
        } else if (type.equals("version")) {
            handlerVersion(dir);
        } else {
            System.out.println("无法处理： " + type);
        }
    }

    public static String checkDirectory(final File directory) {
        String result = "一般目录";
        if (directory == null || !directory.exists()) {
            return null;
        }
        File[] files = directory.listFiles();
        if (files == null) {
            System.out.println("空目录: " + directory.getName());
            return null;
        }
        boolean hasPom = false;
        boolean hasJar = false;
        for (File file : files) {
            if (isVersionDir(file)) {
                System.out.println("===匹配到版本文件目录");
                result = "version";
            }
            boolean pom = FileUtils.isPomFile(file);
            if (pom) {
                System.out.println("===匹配到POM文件");
                hasPom = true;
            }
            boolean jar = FileUtils.isJarFile(file);
            if (jar) {
                System.out.println("===匹配到JAR文件");
                hasJar = true;
            }
            if (hasPom && hasJar) {
                result = "jar&pom";
            }
        }
        return result;
    }

    public static File getJarFile(File dir) {
        if (dir == null || !dir.exists()) {
            return null;
        }
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (isVersionJar(file)) {
                    return file;
                }
            }
        }
        return null;
    }

    public static File getPomFile(File dir) {
        if (dir == null || !dir.exists()) {
            return null;
        }
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (isVersionPom(file)) {
                    return file;
                }
            }
        }
        return null;
    }

    public static boolean isVersionDir(File file) {
        return file.isDirectory()
                && file.getName().equals(Config.VERSION);
    }

    public static boolean isVersionJar(File file) {
        return file.isFile()
                && file.getName().endsWith(Config.VERSION + "-sources.jar");
    }

    public static boolean isVersionPom(File file) {
        return file.isFile()
                && file.getName().endsWith(Config.VERSION + ".pom");
    }
}
